package com.sport.ui.tool;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.text.InputType;
import android.text.method.NumberKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sport.R;
import com.sport.databinding.FragmentToolBmiBinding;
import com.sport.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class BmiFragment extends Fragment {
    private FragmentToolBmiBinding binding;
    private double height, weight = 0f;
    private List<TextView> rangeTextViews = new ArrayList<>(); // 保存需要改变样式的TableRow

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_tool_bmi, container, false);
        binding = FragmentToolBmiBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        init();
        return view;
    }

    public void init() {
        // 设置体重EditText的输入过滤器，限制小数点位数
        binding.weightEditText.setKeyListener(new NumberKeyListener() {
            @NonNull
            @Override
            protected char[] getAcceptedChars() {
                // 允许输入的字符
                return "0123456789.".toCharArray();
            }

            @Override
            public int getInputType() {
                // 输入类型为小数
                return InputType.TYPE_NUMBER_FLAG_DECIMAL;
            }
        });
        binding.heightEditText.setKeyListener(new NumberKeyListener() {
            @NonNull
            @Override
            protected char[] getAcceptedChars() {
                // 允许输入的字符
                return "0123456789.".toCharArray();
            }

            @Override
            public int getInputType() {
                // 输入类型为小数
                return InputType.TYPE_NUMBER_FLAG_DECIMAL;
            }
        });

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String heightStr = binding.heightEditText.getText().toString().trim();
                String weightStr = binding.weightEditText.getText().toString().trim();
                double bmi = calculate(heightStr, weightStr);
                clearStyle();
                getBMIRangeTV(binding.getRoot());
                updateBMIStyle(bmi);

            }
        });
    }

    public double calculate(String heightStr, String weightStr) {
        try {
            height = Double.parseDouble(heightStr);
            weight = Double.parseDouble(weightStr);
            double bmi = weight / (height * 0.01 * height * 0.01);
//            System.out.println(String.format("%.2f", bmi));
            binding.bmiTextView.setText(String.format("%.2f", bmi));
            return bmi;
        } catch (NumberFormatException e) {
            ToastUtil.toastShort(binding.getRoot().getContext(), "身高或者体重输入不能为0哦!");
        }
        return -1;
    }

    // 获取需要改变样式的TextView
    public void getBMIRangeTV(View v) {
        TextView TextView1 = v.findViewById(R.id.tv18);
        TextView TextView2 = v.findViewById(R.id.tv23);
        TextView TextView3 = v.findViewById(R.id.tv27);
        TextView TextView4 = v.findViewById(R.id.tv32);
        TextView TextView5 = v.findViewById(R.id.tv33);
        rangeTextViews.add(0, TextView1);
        rangeTextViews.add(1, TextView2);
        rangeTextViews.add(2, TextView3);
        rangeTextViews.add(3, TextView4);
        rangeTextViews.add(4, TextView5);
    }

    private void updateBMIStyle(double bmi) {
        // 根据BMI值所在区间，设置对应的单元格样式
//        System.out.println("bmi=" + bmi);
        if (bmi < 18.5f) {
            setRangeStyle(0);
        } else if (bmi < 23.9f) {
            setRangeStyle(1);
        } else if (bmi < 27.9f) {
            setRangeStyle(2);
        } else if (bmi < 32.9f) {
            setRangeStyle(3);
        } else {
            setRangeStyle(4);
        }
    }

    // 修改指定序号的单元格样式
    private void setRangeStyle(int index) {
        clearStyle();
        TextView textView = rangeTextViews.get(index);
//        System.out.println("index=" + index);
        textView.setTextColor(ContextCompat.getColor(binding.getRoot().getContext(), R.color.white));
        textView.setBackgroundColor(ContextCompat.getColor(binding.getRoot().getContext(), R.color.colorPrimaryDark));
    }

    // 清空修改的样式
    private void clearStyle() {
//        System.out.println("if clear:");
        for (TextView textView : rangeTextViews) {
            textView.setTextColor(ContextCompat.getColor(binding.getRoot().getContext(), R.color.colorPrimaryDark));
            textView.setBackgroundColor(ContextCompat.getColor(binding.getRoot().getContext(), R.color.white));
        }
    }

    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}